Conditions | 45 |
Paths | 9728 |
Total Lines | 219 |
Code Lines | 135 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like module.exports often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | module.exports = function (args, opts) { |
||
2 | if (!opts) opts = {}; |
||
|
|||
3 | |||
4 | var flags = { bools : {}, strings : {}, unknownFn: null }; |
||
5 | |||
6 | if (typeof opts['unknown'] === 'function') { |
||
7 | flags.unknownFn = opts['unknown']; |
||
8 | } |
||
9 | |||
10 | if (typeof opts['boolean'] === 'boolean' && opts['boolean']) { |
||
11 | flags.allBools = true; |
||
12 | } else { |
||
13 | [].concat(opts['boolean']).filter(Boolean).forEach(function (key) { |
||
14 | flags.bools[key] = true; |
||
15 | }); |
||
16 | } |
||
17 | |||
18 | var aliases = {}; |
||
19 | Object.keys(opts.alias || {}).forEach(function (key) { |
||
20 | aliases[key] = [].concat(opts.alias[key]); |
||
21 | aliases[key].forEach(function (x) { |
||
22 | aliases[x] = [key].concat(aliases[key].filter(function (y) { |
||
23 | return x !== y; |
||
24 | })); |
||
25 | }); |
||
26 | }); |
||
27 | |||
28 | [].concat(opts.string).filter(Boolean).forEach(function (key) { |
||
29 | flags.strings[key] = true; |
||
30 | if (aliases[key]) { |
||
31 | flags.strings[aliases[key]] = true; |
||
32 | } |
||
33 | }); |
||
34 | |||
35 | var defaults = opts['default'] || {}; |
||
36 | |||
37 | var argv = { _ : [] }; |
||
38 | Object.keys(flags.bools).forEach(function (key) { |
||
39 | setArg(key, defaults[key] === undefined ? false : defaults[key]); |
||
40 | }); |
||
41 | |||
42 | var notFlags = []; |
||
43 | |||
44 | if (args.indexOf('--') !== -1) { |
||
45 | notFlags = args.slice(args.indexOf('--')+1); |
||
46 | args = args.slice(0, args.indexOf('--')); |
||
47 | } |
||
48 | |||
49 | function argDefined(key, arg) { |
||
50 | return (flags.allBools && /^--[^=]+$/.test(arg)) || |
||
51 | flags.strings[key] || flags.bools[key] || aliases[key]; |
||
52 | } |
||
53 | |||
54 | function setArg (key, val, arg) { |
||
55 | if (arg && flags.unknownFn && !argDefined(key, arg)) { |
||
56 | if (flags.unknownFn(arg) === false) return; |
||
57 | } |
||
58 | |||
59 | var value = !flags.strings[key] && isNumber(val) |
||
60 | ? Number(val) : val |
||
61 | ; |
||
62 | setKey(argv, key.split('.'), value); |
||
63 | |||
64 | (aliases[key] || []).forEach(function (x) { |
||
65 | setKey(argv, x.split('.'), value); |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | function setKey (obj, keys, value) { |
||
70 | var o = obj; |
||
71 | keys.slice(0,-1).forEach(function (key) { |
||
72 | if (o[key] === undefined) o[key] = {}; |
||
73 | o = o[key]; |
||
74 | }); |
||
75 | |||
76 | var key = keys[keys.length - 1]; |
||
77 | if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') { |
||
78 | o[key] = value; |
||
79 | } |
||
80 | else if (Array.isArray(o[key])) { |
||
81 | o[key].push(value); |
||
82 | } |
||
83 | else { |
||
84 | o[key] = [ o[key], value ]; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | function aliasIsBoolean(key) { |
||
89 | return aliases[key].some(function (x) { |
||
90 | return flags.bools[x]; |
||
91 | }); |
||
92 | } |
||
93 | |||
94 | for (var i = 0; i < args.length; i++) { |
||
95 | var arg = args[i]; |
||
96 | |||
97 | if (/^--.+=/.test(arg)) { |
||
98 | // Using [\s\S] instead of . because js doesn't support the |
||
99 | // 'dotall' regex modifier. See: |
||
100 | // http://stackoverflow.com/a/1068308/13216 |
||
101 | var m = arg.match(/^--([^=]+)=([\s\S]*)$/); |
||
102 | var key = m[1]; |
||
103 | var value = m[2]; |
||
104 | if (flags.bools[key]) { |
||
105 | value = value !== 'false'; |
||
106 | } |
||
107 | setArg(key, value, arg); |
||
108 | } |
||
109 | else if (/^--no-.+/.test(arg)) { |
||
110 | var key = arg.match(/^--no-(.+)/)[1]; |
||
111 | setArg(key, false, arg); |
||
112 | } |
||
113 | else if (/^--.+/.test(arg)) { |
||
114 | var key = arg.match(/^--(.+)/)[1]; |
||
115 | var next = args[i + 1]; |
||
116 | if (next !== undefined && !/^-/.test(next) |
||
117 | && !flags.bools[key] |
||
118 | && !flags.allBools |
||
119 | && (aliases[key] ? !aliasIsBoolean(key) : true)) { |
||
120 | setArg(key, next, arg); |
||
121 | i++; |
||
122 | } |
||
123 | else if (/^(true|false)$/.test(next)) { |
||
124 | setArg(key, next === 'true', arg); |
||
125 | i++; |
||
126 | } |
||
127 | else { |
||
128 | setArg(key, flags.strings[key] ? '' : true, arg); |
||
129 | } |
||
130 | } |
||
131 | else if (/^-[^-]+/.test(arg)) { |
||
132 | var letters = arg.slice(1,-1).split(''); |
||
133 | |||
134 | var broken = false; |
||
135 | for (var j = 0; j < letters.length; j++) { |
||
136 | var next = arg.slice(j+2); |
||
137 | |||
138 | if (next === '-') { |
||
139 | setArg(letters[j], next, arg) |
||
140 | continue; |
||
141 | } |
||
142 | |||
143 | if (/[A-Za-z]/.test(letters[j]) && /=/.test(next)) { |
||
144 | setArg(letters[j], next.split('=')[1], arg); |
||
145 | broken = true; |
||
146 | break; |
||
147 | } |
||
148 | |||
149 | if (/[A-Za-z]/.test(letters[j]) |
||
150 | && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) { |
||
151 | setArg(letters[j], next, arg); |
||
152 | broken = true; |
||
153 | break; |
||
154 | } |
||
155 | |||
156 | if (letters[j+1] && letters[j+1].match(/\W/)) { |
||
157 | setArg(letters[j], arg.slice(j+2), arg); |
||
158 | broken = true; |
||
159 | break; |
||
160 | } |
||
161 | else { |
||
162 | setArg(letters[j], flags.strings[letters[j]] ? '' : true, arg); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | var key = arg.slice(-1)[0]; |
||
167 | if (!broken && key !== '-') { |
||
168 | if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1]) |
||
169 | && !flags.bools[key] |
||
170 | && (aliases[key] ? !aliasIsBoolean(key) : true)) { |
||
171 | setArg(key, args[i+1], arg); |
||
172 | i++; |
||
173 | } |
||
174 | else if (args[i+1] && /true|false/.test(args[i+1])) { |
||
175 | setArg(key, args[i+1] === 'true', arg); |
||
176 | i++; |
||
177 | } |
||
178 | else { |
||
179 | setArg(key, flags.strings[key] ? '' : true, arg); |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | else { |
||
184 | if (!flags.unknownFn || flags.unknownFn(arg) !== false) { |
||
185 | argv._.push( |
||
186 | flags.strings['_'] || !isNumber(arg) ? arg : Number(arg) |
||
187 | ); |
||
188 | } |
||
189 | if (opts.stopEarly) { |
||
190 | argv._.push.apply(argv._, args.slice(i + 1)); |
||
191 | break; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | |||
196 | Object.keys(defaults).forEach(function (key) { |
||
197 | if (!hasKey(argv, key.split('.'))) { |
||
198 | setKey(argv, key.split('.'), defaults[key]); |
||
199 | |||
200 | (aliases[key] || []).forEach(function (x) { |
||
201 | setKey(argv, x.split('.'), defaults[key]); |
||
202 | }); |
||
203 | } |
||
204 | }); |
||
205 | |||
206 | if (opts['--']) { |
||
207 | argv['--'] = new Array(); |
||
208 | notFlags.forEach(function(key) { |
||
209 | argv['--'].push(key); |
||
210 | }); |
||
211 | } |
||
212 | else { |
||
213 | notFlags.forEach(function(key) { |
||
214 | argv._.push(key); |
||
215 | }); |
||
216 | } |
||
217 | |||
218 | return argv; |
||
219 | }; |
||
220 | |||
237 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.